/**
 * 
 */
package org.gov.vis.data.controller;

import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.data.dao.ProgencyDao;
import org.gov.vis.data.dao.ProgencyDaoImpl;
import org.gov.vis.data.dto.FollowUpDetailsDto;
import org.gov.vis.data.dto.ProgencyDto;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author Vishwanath
 *
 */
public class ProgencyCtlrImpl implements ProgencyCtlr {

	/**
	 * Method called to Add or Edit the Progency
	 * @param progencyDto
	 * @throws Exception
	 */
	@Override
	public void addOrEditProgency(final ProgencyDto _progencyDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _progencyCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_code"), _progencyDto.getCode(), 1, 20, true);
			if(_progencyCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_code").toUpperCase(), _progencyCodeErr);

			String _progencyFarmerCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_frmcode"), _progencyDto.getFarmerCode(), 1, 50, true);
			if(_progencyFarmerCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_frmcode").toUpperCase(), _progencyFarmerCodeErr);
			
			String _progencyFarmerNameErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_frmname"), _progencyDto.getFarmerName(), 3, 50, true);
			if(_progencyFarmerNameErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_frmname").toUpperCase(), _progencyFarmerNameErr);
			
			/*
			 * Below are NOT required fields but are validated on their data type 
			 */
			String _progencyNoOfCalvingErr = ValidatorUtils.validateOnlyNumber(rb.getString("dialog_label_prgy_nocalving"), _progencyDto.getNoOfCalving(), 0, Short.MAX_VALUE, false, false);
			if(_progencyNoOfCalvingErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_nocalving").toUpperCase(), _progencyNoOfCalvingErr);
			
			String _progencyAINoErr = ValidatorUtils.validateOnlyNumber(rb.getString("dialog_label_prgy_noaidoneconception"), _progencyDto.getNoOfAI(), 0, Short.MAX_VALUE, false, false);
			if(_progencyAINoErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_noaidoneconception").toUpperCase(), _progencyAINoErr);
			
			String _progencyPrectErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_prntg"), _progencyDto.getPercentage(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_progencyPrectErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_prntg").toUpperCase(), _progencyPrectErr);
			
			String _previousYieldErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_prvlctyldltrs"), _progencyDto.getPreviousYield(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_previousYieldErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_prvlctyldltrs").toUpperCase(), _previousYieldErr);
			
			String _progencyPresentYieldErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_prtmlkyeldpday"), _progencyDto.getPresentYield(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_progencyPresentYieldErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_prtmlkyeldpday").toUpperCase(), _progencyPresentYieldErr);
			
			/*String _progencyMnthyWghtErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwmnthwgt"), _progencyDto.getMonthlyWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_progencyMnthyWghtErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase(), _progencyMnthyWghtErr);
			
			String _progencyBirthWghtErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwbrthwgt"), _progencyDto.getBirthWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_progencyBirthWghtErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase(), _progencyBirthWghtErr);
			*/
			String _progencyDMMilkErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_dmmilk"), _progencyDto.getDmMilkLtr(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_progencyDMMilkErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_dmmilk").toUpperCase(), _progencyDMMilkErr);

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*
			 * Below set of validations check for duplicate records
			 */
			//check if there exists anprogency Progency Object with the same Code
			//If Code has been duplicated, then throw the alert
			ProgencyDto progencyDto = null;
			if(_progencyDto.getPrgyId() > 0){
				//get the ProgencyDto on the passed Code name
				progencyDto = this.getProgencyOnCode(_progencyDto.getCode());
				if(progencyDto != null && progencyDto.getPrgyId() != _progencyDto.getPrgyId()){
					errorMsgs.put(rb.getString("dialog_label_prgy_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_PROGENCY_MSG));
				}
			}//end if
			else{
				//get the ProgencyDto on the passed Code name
				progencyDto = this.getProgencyOnCode(_progencyDto.getCode());
				if(progencyDto != null){
					errorMsgs.put(rb.getString("dialog_label_prgy_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_PROGENCY_MSG));
				}
			}//end else

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final ProgencyDao _progencyDao = new ProgencyDaoImpl();
			//add or edit the Progency
			_progencyDao.addOrEditProgency(_progencyDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			ex.printStackTrace();
			if(_progencyDto.getPrgyId() > 0){
				throw new SystemException(rb.getString("data.editProgency.exception"));
			}
			else{
				throw new SystemException(rb.getString("data.addProgency.exception"));
			}
		}
	}//end addOrEditProgency

	/**
	 * Method called to delete the Progency Object based on the passed progency Id
	 * @param _progencyId
	 * @throws Exception
	 */
	@Override
	public void deleteProgency(final long _progencyId) throws Exception {
		try{
			final ProgencyDao _progencyDao = new ProgencyDaoImpl();
			_progencyDao.deleteProgency(_progencyId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString("data.deleteProgency.exception"));
		}
	}//end deleteProgency

	/**
	 * Method called to get the Progency based on the passed primary key Id
	 * @param _progencyId
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProgencyDto getProgency(final long _progencyId) throws Exception {
		try{
			final ProgencyDao _progencyDao = new ProgencyDaoImpl();
			ProgencyDto _progencyDto = new ProgencyDto();
			_progencyDto.setPrgyId(_progencyId);

			ProgencyDto progencyDto = null;
			List<ProgencyDto> _progencyList = _progencyDao.getProgencies(_progencyDto);
			if(_progencyList != null && !_progencyList.isEmpty()){
				progencyDto = _progencyList.get(0);
			}

			return progencyDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString("data.viewEditProgency.exception"));
		}
	}//end getProgency

	/**
	 * Method called to get the Progency based on the passed Code
	 * @param _progencyCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProgencyDto getProgencyOnCode(final String _code) throws Exception {
		final ProgencyDao _progencyDao = new ProgencyDaoImpl();
		ProgencyDto _progencyDto = new ProgencyDto();
		_progencyDto.setCode(_code);

		ProgencyDto progencyDto = null;
		List<ProgencyDto> _progencyList = _progencyDao.getProgencies(_progencyDto);
		if(_progencyList != null && !_progencyList.isEmpty()){
			progencyDto = _progencyList.get(0);
		}

		return progencyDto;
	}//end getProgencyOnCode

	/**
	 * Method called to get the Progency based on the passed Farmer Code
	 * @param _progencyCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProgencyDto getProgencyOnFarmerCode(String farmerCode)
	throws Exception {
		final ProgencyDao _progencyDao = new ProgencyDaoImpl();
		ProgencyDto _progencyDto = new ProgencyDto();
		_progencyDto.setFarmerCode(farmerCode);

		ProgencyDto progencyDto = null;
		List<ProgencyDto> _progencyList = _progencyDao.getProgencies(_progencyDto);
		if(_progencyList != null && !_progencyList.isEmpty()){
			progencyDto = _progencyList.get(0);
		}

		return progencyDto;
	}//end getProgencyOnFarmerCode

	/**
	 * Method called to get the Progency based on the passed Farmer Name
	 * @param _progencyCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProgencyDto getProgencyOnFarmerName(String farmerName)
	throws Exception {
		final ProgencyDao _progencyDao = new ProgencyDaoImpl();
		ProgencyDto _progencyDto = new ProgencyDto();
		_progencyDto.setFarmerName(farmerName);

		ProgencyDto progencyDto = null;
		List<ProgencyDto> _progencyList = _progencyDao.getProgencies(_progencyDto);
		if(_progencyList != null && !_progencyList.isEmpty()){
			progencyDto = _progencyList.get(0);
		}

		return progencyDto;
	}//end getProgencyOnFarmerName

	/**
	 * Method called to get the Progency based on the passed Breed
	 * @param _progencyCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProgencyDto getProgencyOnBreedType(final String _breedType) throws Exception {
		final ProgencyDao _progencyDao = new ProgencyDaoImpl();
		ProgencyDto _progencyDto = new ProgencyDto();
		_progencyDto.setBreedType(_breedType);

		ProgencyDto progencyDto = null;
		List<ProgencyDto> _progencyList = _progencyDao.getProgencies(_progencyDto);
		if(_progencyList != null && !_progencyList.isEmpty()){
			progencyDto = _progencyList.get(0);
		}

		return progencyDto;
	}//end getProgencyOnBreed

	/**
	 * Method called to get the Progency based on the passed BullNo
	 * @param _progencyCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProgencyDto getProgencyOnBullNo(final String _bullNo) throws Exception {
		final ProgencyDao _progencyDao = new ProgencyDaoImpl();
		ProgencyDto _progencyDto = new ProgencyDto();
		_progencyDto.setBullNo(_bullNo);

		ProgencyDto progencyDto = null;
		List<ProgencyDto> _progencyList = _progencyDao.getProgencies(_progencyDto);
		if(_progencyList != null && !_progencyList.isEmpty()){
			progencyDto = _progencyList.get(0);
		}

		return progencyDto;
	}//end getProgencyOnBullNo

	/**
	 * Method called to get the Progency based on the passed Progency Object
	 * @param progencyDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProgencyDto getProgency(final ProgencyDto _progencyDto) throws Exception {
		final ProgencyDao _progencyDao = new ProgencyDaoImpl();

		ProgencyDto progencyDto = null;
		List<ProgencyDto> _progencyList = _progencyDao.getProgencies(_progencyDto);
		if(_progencyList != null && !_progencyList.isEmpty()){
			progencyDto = _progencyList.get(0);
		}

		return progencyDto;
	}//end getProgencies

	/**
	 * Method called to get the list of Progency based on the passed Progency Object
	 * @param progencyDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ProgencyDto> getProgencies(final ProgencyDto _progencyDto) throws Exception {
		final ProgencyDao _progencyDao = new ProgencyDaoImpl();
		return _progencyDao.getProgencies(_progencyDto);
	}//end getProgencies

	/**
	 * Method called to search for the Progencies based on the passed Progency Object
	 * @param progencyDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ProgencyDto> searchProgencies(ProgencyDto _progencyDto) throws Exception {
		try{
			final ProgencyDao _progencyDao = new ProgencyDaoImpl();
			return _progencyDao.searchProgencies(_progencyDto);
		}
		catch (Exception ex) {
			ex.printStackTrace();
			throw new SystemException(rb.getString("data.viewMaintainProgency.exception"));
		}
	}//end searchProgencies
	
	/**
	 *Method called to either Add or Edit a Follow Up Details
	 */
	public void addOrEditFollowUpProgency(final FollowUpDetailsDto _followUpProgencyDto) throws Exception{
		try{
			final ProgencyDao _progencyDao = new ProgencyDaoImpl();
			_progencyDao.addOrEditFollowUpProgency(_followUpProgencyDto);
		}
		catch (Exception ex) {
			ex.printStackTrace();
			throw new SystemException(rb.getString("data.viewMaintainProgency.exception"));
		}
	}
	
	/**
	 *Method called to Delete a Follow Up Progency
	 */
	public void deleteFollowUpProgency(final long _followUpProgencyId) throws Exception{
		try{
			final ProgencyDao _progencyDao = new ProgencyDaoImpl();
			_progencyDao.deleteFollowUpProgency(_followUpProgencyId);
		}
		catch (Exception ex) {
			ex.printStackTrace();
			throw new SystemException(rb.getString("data.viewMaintainProgency.exception"));
		}
	}

}//end class
